package me.awei.toolbar.view;

import me.awei.toolbar.ResourceTable;
import me.awei.toolbar.drawable.TabSwitcherDrawable;
import me.awei.toolbar.util.Condition;
import me.awei.toolbar.util.DimensionUtil;
import me.awei.toolbar.util.PixelUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.Resource;

public class TabSwitcherButton extends StackLayout {
    /**
     * The default text size of the displayed label in pixels.
     */
    private int textSizeNormal;
    /**
     * The text size of the displayed label, which is used when displaying a value greater than 99,
     * in pixels.
     */
    private int textSizeSmall;
    /**
     * The currently displayed label.
     */
    private String label = "0";

    private Image image;

    private Text text;

    private TabSwitcherDrawable drawable;


    public TabSwitcherButton(final Context context) {
        this(context, null);
    }


    public TabSwitcherButton(final Context context,
                             final AttrSet attributeSet) {
        this(context, attributeSet, "");
    }


    public TabSwitcherButton(final Context context,
                             final AttrSet attributeSet,
                             final String defaultStyle) {
        super(context, attributeSet, defaultStyle);
        initialize(context);
        initImage();
        initText();
    }

    private void initialize(Context context) {
        setClickable(true);
        setFocusable(FOCUS_ENABLE);
        textSizeNormal = (int) PixelUtil.fp2px(DimensionUtil.parseDimension(context.getString(ResourceTable.String_tab_switcher_drawable_font_size_normal)));
        textSizeSmall = (int) PixelUtil.fp2px(DimensionUtil.parseDimension(context.getString(ResourceTable.String_tab_switcher_drawable_font_size_small)));
        label = Integer.toString(0);
    }

    private void initImage() {
        image = new Image(getContext());

        Resource resource = convertRes(getContext(), ResourceTable.Media_tab_switcher_drawable_background);
        //The drawable, which is used by the image button.
        drawable = new TabSwitcherDrawable(getContext(), resource);
        image.setImageElement(drawable);
        LayoutConfig imageParams = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        imageParams.alignment = TextAlignment.CENTER;
        addComponent(image, imageParams);
    }

    public void setButtonColor(float matrix, Color textColor) {
        if (drawable != null) {
            drawable.setColorMatrix(matrix);
        }
        if (text != null) {
            text.setTextColor(textColor);
        }

    }

    private void initText() {
        text = new Text(getContext());
        text.setTextColor(Color.DKGRAY);
        text.setTextSize(textSizeNormal);
        text.setFont(Font.SANS_SERIF);
        text.setText(label);
        text.setTextAlignment(TextAlignment.CENTER);

        LayoutConfig textParams = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        textParams.alignment = TextAlignment.CENTER;
        addComponent(text, textParams);
    }


    private Resource convertRes(Context mContext, int drawableRes) {
        Resource resource = null;
        try {
            resource = mContext.getResourceManager().getResource(drawableRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resource;
    }


    public final void setCount(final int count) {
        Condition.INSTANCE.ensureAtLeast(count, 0, "The count must be at least 0");
        label = Integer.toString(count);
        text.setText(label);
        if (label.length() > 2) {
            label = "99+";
            text.setTextSize(textSizeSmall);
        } else {
            text.setTextSize(textSizeNormal);
        }
    }
}
